﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Entity;
using System.Collections.Generic;
using Job.Logic.Company;
using Job.Logic.Common;

namespace Job.Web.Company
{
    public partial class CompanyManager : BasePage
    {
        int rsubjectId = myR["subjectid"].IntValue;
        int edit = myR["edit"].IntValue;
        int reset = myR["reset"].IntValue;

        private int CompanyId
        {
            get
            {
                //后台管理员能通过后台为企业增加职位，所以在此处需要兼职企业ID从url或从Session中获取
                int id = myR["company"].IntValue;
                if (id > 0) return id;
                MySession session = new MySession();
                SessionCompany sCompany = session.GetCompanyFromSession();
                if (null == sCompany)
                    return 0;
                else
                    return sCompany.CompanyId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int logout = myR["logout"].IntValue;
            if ( logout == 1)
            {
                new MySession().RemoveCompanyFromSession();
                String url = WebCommon.GetApplicationPath(Request) + "Index.aspx";
                Response.Redirect(url);
                return ;
            }

            if (reset == 1)
            {
                tbMain.Visible = false;
                tbForgetPwd.Visible = true;
            }
            else
            {
                tbMain.Visible = true;
                tbForgetPwd.Visible = false;
                if (0 == CompanyId)
                {
                    Response.Redirect("CompanyLogin.aspx?categoryid=2&error=1");
                    return;
                }
                this.LoadControls();
            }
        }

        private void LoadControls()
        {
            string fileName = "~/userControls/Company/CompanyView.ascx";
            switch (rsubjectId)
            {
                case (int)Constants.CompanySubject.BaseInfo:
                    fileName = "~/userControls/Company/CompanyInfo.ascx";
                    break;
                case (int)Constants.CompanySubject.CompanyIntro:
                case (int)Constants.CompanySubject.ProductIntro:
                    fileName = "~/userControls/Company/Introduction.ascx";
                    break;
                case (int)Constants.CompanySubject.JobPosition:
                    if ( edit == 1)
                        fileName = "~/userControls/Company/PositionInfo.ascx";
                    else
                        fileName = "~/userControls/Company/PositionList.ascx";
                    break;
                case (int)Constants.CompanySubject.CompanyLogo:
                    fileName = "~/userControls/Company/CompanyLogo.ascx";
                    break;
                case (int)Constants.CompanySubject.PasswordEdit:
                    fileName = "~/userControls/Company/ChangePassword.ascx";
                    break;
            }

            this.LoadCtrl(fileName, "phManager");
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                String email = txtEmail.Text.Trim();
                IList<JobCompanyInfo> list = new CompanyLogic().GetCompanyByEmail(email);
                if (list == null || list.Count == 0)
                {
                    this.lblMessage.Text = "您所输入的邮箱并未在本网站注册！";
                }
                else if (list.Count > 1)
                {
                    this.lblMessage.Text = "您所输入的邮箱在本网站被多个用户注册，无法确定您所使用的账号！";
                }
                else
                {
                    JobCompanyInfo company = list[0];
                    MailObject obj = new MailObject()
                    {
                        MailContent =  "<br> 您好！<br> 您在无忧才智网站的登录密码为：<strong>"
                            + company.LoginPswd
                            + "</strong>，请点击<a href='http://www.wyhrm.com' target='_blank'>这里</a>登录！感谢您对我们的关心与支持。",
                        Subject = "无忧才智登录密码",
                        MailTo = email
                    };
                    CommonLogic.SendMail(obj);
                    lblMessage.Text = "已将您的登录密码发到到邮箱，请查看邮件获得密码！";
                }
            }
            catch (Exception ex)
            {
                this.lblMessage.Text = "系统错误，取回密码失败！";                
            }
            this.lblMessage.Visible = true;
        }
    }
}
